<?php
/**
 * @author Matteo Vigoni <mattevigo@gmail.com>
 * @package INTVWeb	
 * @version 1.0
 * 
 * Classe Video
 */
require_once("DBEntity.php");

class Video extends DBEntity
{
	// tabella
	const TABLE = 'crystal_videos';
	const PRIMARY_KEY = 'video_id';
	
	/**
	 * Costruttore 
	 * 
	 * @param $db
	 * @param $id
	 * @return unknown_type
	 */
	public function __construct(DB $db, $id=null)
	{
		parent::__construct($db, self::TABLE, self::PRIMARY_KEY, $id);
	}
	
	/**
	 * Parse the YouTube video id from the link
	 * 
	 * @return string the youtube id
	 */
	public function parseId()
	{
		$video = $this->get('video_link');
		$video = str_split($video, 31);
		
		return $video[1];
	}
	
	/**
	 * Restituisce l'ultimo video della settimana
	 * 
	 * @return unknown_type
	 */
	public static function getVideoOfTheWeek(DB $db)
	{
		$table = self::TABLE;
		$mysql = " SELECT video_id FROM `$table` ORDER BY video_id DESC LIMIT 1 ";
		
		try
		{
			$result = $db->query($mysql);
			$row = mysql_fetch_row($result);
			return new Video($db, $row[0]);
		}
		catch (DBException $e)
		{
			echo $e->getMessage();
			return null;
		}
	}
}